﻿using AbpCodeGenerater.Core;
using Scriban;
using Scriban.Runtime;

namespace AbpCodeGenerater
{
    public partial class SimpleArchiveGenerateWindow : Form
    {
        protected AbpDomainClass AbpDomainClass { get; set; }

        protected TemplateContext TemplateContext { get; }

        protected string TemplateDirectoryPath
        {
            get
            {
                return Path.Combine(Environment.CurrentDirectory, "Templates\\SimpleArchive");
            }
        }
        public SimpleArchiveGenerateWindow(AbpDomainClass abpDomainClass)
        {
            InitializeComponent();
            AbpDomainClass = abpDomainClass;

            var scriptObject = new ScriptObject();
            scriptObject.Import(typeof(TemplateFuns));
            scriptObject.Import(typeof(TemplateModelExtension));
            var templateModel = new TemplateModel(abpDomainClass);
            scriptObject.Import(templateModel);
            TemplateContext = new TemplateContext(scriptObject);

            if (AbpDomainClass?.DomainProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtIRepositoryPath.Text = Path.Combine(AbpDomainClass.DomainProject.FullPath, AbpDomainClass.DirectoryPath, $"I{AbpDomainClass.Name}Repository.cs");
            }

            if (AbpDomainClass?.DomainProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtSettingProvider.Text = Path.Combine(AbpDomainClass.DomainProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}SettingProvider.cs");
            }

            if (AbpDomainClass?.DomainProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtDomainService.Text = Path.Combine(AbpDomainClass.DomainProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}Manager.cs");
            }

            if (AbpDomainClass?.EntityFrameworkCoreProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtModelConfiguration.Text = Path.Combine(AbpDomainClass.EntityFrameworkCoreProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}ModelConfiguration.cs");
            }

            if (AbpDomainClass?.EntityFrameworkCoreProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtRepositoryPath.Text = Path.Combine(AbpDomainClass.EntityFrameworkCoreProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}Repository.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtEntityDto.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}Dto.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtCreateOrUpdateBase.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}CreateOrUpdateBase.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtCreateDto.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}CreateDto.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtUpdateDto.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}UpdateDto.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtIAppService.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, AbpDomainClass.DirectoryPath, $"I{AbpDomainClass.Name}AppService.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtPermissionDefinition.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, "Permissions", AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}PermissionDefinitionPovider.cs");
            }

            if (AbpDomainClass?.ApplicationContractsProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtPermissionNames.Text = Path.Combine(AbpDomainClass.ApplicationContractsProject.FullPath, "Permissions", AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}Permissions.cs");
            }

            if (AbpDomainClass?.ApplicationProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtAppService.Text = Path.Combine(AbpDomainClass.ApplicationProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}AppService.cs");
            }

            if (AbpDomainClass?.ApplicationProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtAutoMapper.Text = Path.Combine(AbpDomainClass.ApplicationProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}AutoMapperProfile.cs");
            }

            if (AbpDomainClass?.HttpApiProject?.FullPath != null && AbpDomainClass.DirectoryPath != null)
            {
                txtController.Text = Path.Combine(AbpDomainClass.HttpApiProject.FullPath, AbpDomainClass.DirectoryPath, $"{AbpDomainClass.Name}Controller.cs");
            }

            if (AbpDomainClass?.DomainSharedProject?.FullPath != null && AbpDomainClass.Name != null && templateModel.ProjectGroupName != null && templateModel.ParentMenu != null)
            {
                txtModelResources.Text = Path.Combine(AbpDomainClass.DomainSharedProject.FullPath, "Localization", templateModel.ProjectGroupName, templateModel.ParentMenu, AbpDomainClass.Name, $"{AbpDomainClass.Name}ModelResources.cs");
            }

            if (AbpDomainClass?.DomainSharedProject?.FullPath != null && AbpDomainClass.Name != null && templateModel.ProjectGroupName != null && templateModel.ParentMenu != null)
            {
                txtResourcesZhCn.Text = Path.Combine(AbpDomainClass.DomainSharedProject.FullPath, "Localization", templateModel.ProjectGroupName, templateModel.ParentMenu, AbpDomainClass.Name, $"zh-Hans.json");
            }
        }

        private void btnGenerate_Click(object sender, EventArgs e)
        {
            if (cboxIRepository.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "IRepository.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtIRepositoryPath.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxSettingProvider.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "SettingProvider.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtSettingProvider.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxDomainService.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "DomainService.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtDomainService.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxModelConfiguration.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "ModelConfiguration.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtModelConfiguration.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxRepository.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "Repository.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtRepositoryPath.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxEntityDto.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "EntityDto.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtEntityDto.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxCreateOrUpdateBase.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "CreateOrUpdateBase.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtCreateOrUpdateBase.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxCreateDto.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "CreateDto.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtCreateDto.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxUpdateDto.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "UpdateDto.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtUpdateDto.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxIAppService.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "IAppService.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtIAppService.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxPermissionDefinition.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "PermissionDefinition.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtPermissionDefinition.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxPermissionNames.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "PermissionNames.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtPermissionNames.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxAppService.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "AppService.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtAppService.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxAutoMapper.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "AutoMapper.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtAutoMapper.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxController.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "Controller.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtController.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxModelResources.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "ModelResources.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtModelResources.Text.MakeSurePathExists(), fileContent);
            }

            if (cboxResourcesZhCn.Checked)
            {
                var templateContent = File.ReadAllText(Path.Combine(TemplateDirectoryPath, "ResourcesZhCn.t"));
                var template = Template.Parse(templateContent);
                var fileContent = template.Render(TemplateContext);
                File.WriteAllText(txtResourcesZhCn.Text.MakeSurePathExists(), fileContent);
            }

            MessageBox.Show("生成成功！");
        }
    }
}
